<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="日志类型">
              <a-select v-model="logTypeId" placeholder="请选择" @select="typeClick">
                <a-select-option :value="item.value" v-for="(item,index) in optionType" :key="index" >{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-range-picker
              :ranges="{' 今天': [moment(), moment()], '一月内': [moment(), moment().endOf('month')] }"
              @change="onChange"
            />
          </a-col>
          <a-col :md="8" :sm="24">
            <a-input-search placeholder="请输入类型关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :pagination="pagination"
    >
      <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
      <span slot="logTypeName" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="logContent" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="operatorName" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="createTime" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { systemlogList } from '@/api/system'
import utils from '@/utils/common'
import { mapActions } from 'vuex'
import moment from 'moment'
var timeStamp = Number(new Date())

export default {
  name: 'SystemLog',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      logTypeId: '',
      optionType: [
        { value: '', label: '全部' }
      ],
      // 查询参数
      queryParam: {},
      keyword: '',
      startTime: '',
      endTime: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '日志类型',
          dataIndex: 'logTypeName',
          scopedSlots: { customRender: 'logTypeName' }
        },
        {
          title: '日志内容',
          dataIndex: 'logContent',
          scopedSlots: { customRender: 'logContent' }
        },
        {
          title: '操作人',
          dataIndex: 'operatorName',
          scopedSlots: { customRender: 'operatorName' }
        },
        {
          title: '操作时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return systemlogList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      keyword: this.keyword,
      logTypeId: this.logTypeId,
      startTime: this.startTime,
      endTime: this.endTime
    }
    this.$nextTick(() => {
      this.ApiType()
    })
  },
  methods: {
    ...mapActions(['SystemlogTypeList']),
    moment,
    typeClick (val) { // 类型
      this.queryParam.logTypeId = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    onChange (dates, dateStrings) { // 日期
      this.queryParam.startTime = dateStrings[0]
      this.queryParam.endTime = dateStrings[1]
      this.$refs.table.refresh()
    },
    ApiType () { // 操作类型
      this.SystemlogTypeList()
        .then((res) => {
          if (res.status === 200 || res.status === '200') {
            if (res.data.list && res.data.list.length !== 0) {
              var arr = []
              for (var i in res.data.list) {
                arr.push({
                  value: res.data.list[i].id,
                  label: res.data.list[i].name
                })
              }
              this.optionType = this.optionType.concat(arr)
            }
          } else {
            this.requestFailed(res)
          }
        })
        .catch(err => this.requestFailed(err))
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
